package com.nsyue.auth.domain.user;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nsyue.auth.constant.SysConstant;
import com.nsyue.auth.controller.result.ApiStatusEnum;
import com.nsyue.auth.entity.*;
import com.nsyue.auth.exception.NsyueAuthException;
import com.nsyue.auth.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author laixm
 * @Date 2022/11/24 下午 1:36
 * @ClassName UserDetailsServiceImpl
 * @Description
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername,username));
        if (user == null){
            throw new NsyueAuthException(ApiStatusEnum.USER_CODE_ERROR);
        }
        //获取用户权限
        List<UserRole> userRoleList = userRoleService.list(new QueryWrapper<UserRole>().lambda().eq(UserRole::getUserId,user.getId()));
        if (ObjectUtil.isEmpty(userRoleList)){
            throw new NsyueAuthException(ApiStatusEnum.NO_ROLE);
        }
        List<Long> roleIdList = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList());
        List<Role> roleList = roleService.list(new QueryWrapper<Role>().lambda()
                .in(Role::getId,roleIdList).eq(Role::getStatus, SysConstant.STR_Z_ONE));
        if (ObjectUtil.isEmpty(roleList)){
            throw new NsyueAuthException(ApiStatusEnum.NO_ROLE);
        }
        List<RolePermission> rolePermissionList = rolePermissionService.list(new QueryWrapper<RolePermission>().lambda()
                .in(RolePermission::getRoleId,roleIdList));
        if (ObjectUtil.isEmpty(rolePermissionList)){
            throw new NsyueAuthException(ApiStatusEnum.UNAUTHORIZED);
        }
        List<Long> permissionIdList = rolePermissionList.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
        List<Permission> permissionList = permissionService.list(new QueryWrapper<Permission>().lambda()
                .in(Permission::getId,permissionIdList).eq(Permission::getStatus,SysConstant.STR_Z_ONE));
        if (ObjectUtil.isEmpty(permissionList)){
            throw new NsyueAuthException(ApiStatusEnum.UNAUTHORIZED);
        }

        String collect = permissionList.stream().map(Permission::getPermissionCode).collect(Collectors.joining(","));
        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(collect));
    }

}
